package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.utils.ArchivesClassTreeUtil;
import com.tuocent.dagv2.common.utils.SnowFlakeID;
import com.tuocent.dagv2.entity.ArchivesClass;
import com.tuocent.dagv2.entity.ArchivesClassify;
import com.tuocent.dagv2.mapper.ArchivesClassMapper;
import com.tuocent.dagv2.service.IArchivesClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuocent.dagv2.service.IArchivesClassifyService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案分类 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-07-31
 */
@Service
public class ArchivesClassServiceImpl extends ServiceImpl<ArchivesClassMapper, ArchivesClass> implements IArchivesClassService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private SnowFlakeID snowFlakeID;
    @Autowired
    private IArchivesClassifyService archivesClassifyService;


    @Override
    public AjaxResult getArchivesClassData() {
        List<ArchivesClass> classData = ArchivesClassTreeUtil.build(getBaseMapper()
                .getClassData(new QueryWrapper<ArchivesClass>().eq("a.is_del", 0)), "0");
        return AjaxResult.success(classData);
    }

    @Override
    public String setArchivesClass(Model model, String pid, String tid, String id, String parentId) {
        ArchivesClass data = getOne(new QueryWrapper<ArchivesClass>()
                .eq("id", id)
                .eq("parent_id", parentId));
        if (data == null) {
            data = new ArchivesClass();
            data.setPid(pid);
            data.setTid(tid);
            if (!parentId.isEmpty()) {
                ArchivesClass pData = getOne(new QueryWrapper<ArchivesClass>()
                        .eq("is_del", "0")
                        .eq("id", parentId));
                if (pData == null) {
                    model.addAttribute("message", "没有找到上级分类");
                    return "/home/error";
                }
                data.setPcode(pData.getFullCode());
                data.setParentId(parentId);
                data.setClassCode(pData.getClassCode() + 1);
            } else {
                data.setParentId("0");
                data.setClassCode(0);
            }
        }
        List<ArchivesClassify> classifyList = archivesClassifyService.list(
                new QueryWrapper<ArchivesClassify>()
                        .eq("isdel", "0"));
        model.addAttribute("classifyList", classifyList);
        model.addAttribute("data", data);
        return "/basic/setArchivesClass";
    }

    @Override
    public AjaxResult saveArchivesClass(ArchivesClass data) {
        if ("0".equals(data.getParentId()) && (data.getCode() == null || "".equals(data.getCode()))) {
            return AjaxResult.error("请输入分类编号");
        }
        if (data.getName() == null || "".equals(data.getName())) {
            return AjaxResult.error("请输入分类名称");
        }
        if (data.getPcode() == null) {
            data.setPcode("");
        }
        QueryWrapper<ArchivesClass> queryWrapper = new QueryWrapper<>();
        Long count = 0L;
        if (data.getCode() != null && !"".equals(data.getCode())) {
            queryWrapper.eq("code", data.getCode())
                    .eq("parent_id", data.getParentId())
                    .eq("is_del", "0")
                    .ne("id", data.getId());
            count = this.count(queryWrapper);
            if (count > 0) {
                return AjaxResult.error("该分类号已存在");
            }
        }

        queryWrapper.clear();
        queryWrapper.eq("parent_id", data.getParentId())
                .eq("name", data.getName())
                .eq("is_del", "0")
                .ne("id", data.getId());
        count = this.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error("该分类名称已存在");
        }
        if ("".equals(data.getId())) {
            data.setIsDel("0");
            data.setId(snowFlakeID.getId());
        }
        if ("".equals(data.getPcode())) {
            data.setFullCode(data.getCode());
        } else {
            data.setFullCode(data.getPcode() + data.getDoc() + data.getCode());
        }

        saveOrUpdate(data);
        updateBatchById(ArchivesClassTreeUtil.modifyClassCode(list(new QueryWrapper<ArchivesClass>().eq("is_del", 0)), "0"));

        try {
            return AjaxResult.success("编辑档案分类成功");
        } catch (Exception e) {
            return AjaxResult.error("编辑档案分类失败，错误信息：" + e.getMessage());
        }
    }

    @Override
    public AjaxResult delArchivesClass(String id) {
        if (count(new QueryWrapper<ArchivesClass>().eq("parent_id", id).eq("is_del", 0)) > 0) {
            return AjaxResult.error("该分类还有下级数据，不可删除");
        }
        return update(new UpdateWrapper<ArchivesClass>().set("is_del", 1).eq("id", id)) ? AjaxResult.success("删除档案分类成功") : AjaxResult.error("删除档案分类失败");
    }
}
